//
//  YSContractStampedViewController.m
//  YaSha-iOS
//
//  Created by YaSha_Tom on 2019/2/21.
//  Copyright © 2019年 亚厦装饰股份有限公司. All rights reserved.
//

#import "YSContractStampedViewController.h"
#import "YSAttachmentClassificationVC.h"
#import "YSNewsAttachmentModel.h"

@interface YSContractStampedViewController ()
@property (nonatomic, strong) NSMutableArray *basicInformationArr;
@property (nonatomic, strong) NSMutableArray *attachmentArray;


@end

@implementation YSContractStampedViewController
#pragma mark -- 懒加载
- (NSMutableArray *)basicInformationArr {
    if (!_basicInformationArr) {
        _basicInformationArr = [NSMutableArray array];
    }
    return _basicInformationArr;
}
- (NSMutableArray *)attachmentArray {
    if (!_attachmentArray) {
        _attachmentArray = [NSMutableArray array];
    }
    return _attachmentArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self doNetworking];
}

- (void)doNetworking {
    NSString *urlString = [NSString stringWithFormat:@"%@%@/%@",YSDomain,getSealOfContractInfoApi,self.cellModel.businessKey];
    [QMUITips showLoadingInView:self.view];
    DLog(@"========%@",urlString);
    [YSNetManager ys_request_GETWithUrlString:urlString isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"编制调整申请详情:%@",response);
        [QMUITips hideAllTipsInView:self.view];
        if ([response[@"code"] intValue] == 1) {
            self.flowFormModel = [YSFlowFormModel yy_modelWithJSON:response[@"data"]];
            self.coverNavView.titleLabel.text = self.flowFormModel.baseInfo.title;
            [self.functionHeaderView setHeaderModel:self.flowFormModel.baseInfo];
            [self setUpData:self.flowFormModel.info];
            [self.attachmentArray addObject:[NSArray yy_modelArrayWithClass:[YSNewsAttachmentModel class] json:self.flowFormModel.info[@"pubAttList"]]];
            [self.attachmentArray addObject:[NSArray yy_modelArrayWithClass:[YSNewsAttachmentModel class] json:self.flowFormModel.info[@"supplierList"]]];
            [self.attachmentArray addObject:[NSArray yy_modelArrayWithClass:[YSNewsAttachmentModel class] json:self.flowFormModel.info[@"qpList"]]];
            NSLog(@"======%@",self.attachmentArray);
            if (self.attachArray.count > 0) {
                [self.functionHeaderView.documentButton setTitle:[NSString stringWithFormat:@"关联文档(%lu)",(unsigned long)self.attachArray.count] forState:UIControlStateNormal];
            }
            if ([self.flowFormModel.info[@"postscriptList"] count]>0) {
                [self.postscriptArray addObject:@{@"title":@" ",@"content":self.flowFormModel.info[@"postscriptList"],@"special":@"4"}];
            }
            [self.tableView reloadData];
            //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
            dispatch_async(dispatch_get_main_queue(), ^{
                [self markSectionHeaderLocation];
            });
        }
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
    
    
    //提交者附言
    NSString *urlStringPS = [NSString stringWithFormat:@"%@%@/%@", YSDomain, getPostscriptListApi, self.cellModel.businessKey];
    NSLog(@"------%@",urlStringPS);
    [YSNetManager ys_request_GETWithUrlString:urlStringPS isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"获取提交者附言:%@", response);
        if ([response[@"code"] intValue] == 1) {
            self.postscriptArray =[[YSDataManager getFlowRecordListData:response] copy];
            for (NSDictionary *dic in response[@"data"]) {
                [self.attachArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[YSNewsAttachmentModel class] json:dic[@"mobileFileVos"]]];
            }
            if (self.attachArray.count > 0) {
                [self.functionHeaderView.documentButton setTitle:[NSString stringWithFormat:@"关联文档(%lu)",self.attachArray.count] forState:UIControlStateNormal];
            }
          [self.tableView reloadData];
           
        }
    } failureBlock:^(NSError *error) {
        DLog(@"error:%@", error);
    } progress:nil];
    
    //审批记录
    NSString *urlStringRecord = [NSString stringWithFormat:@"%@%@/%@/1", YSDomain, getCommentsByProcessInstanceIdApi, self.cellModel.processInstanceId];
    DLog(@"=========%@",urlString);
    [YSNetManager ys_request_GETWithUrlString:urlStringRecord isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"审批记录:%@", response);
        self.recordArray = [[YSDataManager getFlowRecordListData:response] mutableCopy];
        [self.tableView reloadData];
        //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
        dispatch_async(dispatch_get_main_queue(), ^{
            [self markSectionHeaderLocation];
        });
        
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
    
    //转阅记录列表
    NSString *urlStringTurn = [NSString stringWithFormat:@"%@%@/%@/2", YSDomain, getCommentsByProcessInstanceIdApi, self.cellModel.processInstanceId];
    DLog(@"=========%@",urlString);
    [YSNetManager ys_request_GETWithUrlString:urlStringTurn isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"转阅记录列表:%@", response);
        self.turnArray = [[YSDataManager getFlowRecordListData:response] mutableCopy];
        [self.tableView reloadData];
        //定位当前的标题位置（该计算要在tableView刷新之后计算来保证header位置的准确）
        dispatch_async(dispatch_get_main_queue(), ^{
            [self markSectionHeaderLocation];
        });
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
    
    //关联流程
    NSString *urlStringAssociated = [NSString stringWithFormat:@"%@%@/%@", YSDomain, getRelateFlowVo,self.cellModel.businessKey];
    [YSNetManager ys_request_GETWithUrlString:urlStringAssociated isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"关联流程列表:%@", response);
        self.associaterArr = [YSDataManager getFlowListData:response];
        if (self.associaterArr.count > 0) {
            [self.functionHeaderView.flowButton setTitle:[NSString stringWithFormat:@"关联流程(%lu)",(unsigned long)self.associaterArr.count] forState:UIControlStateNormal];
        }
    } failureBlock:^(NSError *error) {
        
    } progress:nil];
    
    //关联文档
    NSString *urlStringDocument = [NSString stringWithFormat:@"%@%@/%@", YSDomain, getAttachmentApi, self.cellModel.businessKey];
    [YSNetManager ys_request_GETWithUrlString:urlStringDocument isNeedCache:NO parameters:nil successBlock:^(id response) {
        DLog(@"获取关联文档:%@", response);
        [self.attachArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[YSNewsAttachmentModel class] json:response[@"data"]]];
//        self.documentAr = [[YSDataManager getFlowAttachFileListData:response] mutableCopy];
        if (self.attachArray.count > 0) {
            [self.functionHeaderView.documentButton setTitle:[NSString stringWithFormat:@"关联文档(%lu)",self.attachArray.count] forState:UIControlStateNormal];
        }
    } failureBlock:^(NSError *error) {
        DLog(@"error:%@", error);
    } progress:nil];
}

- (void)setUpData:(NSDictionary *)dic {
    [self.basicInformationArr addObject:@{@"title":@"基本信息",@"content":@" ",@"special":@"1"}];
    [self.basicInformationArr addObject:@{@"title":@"表单编号",@"content":dic[@"code"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"采购合同名称",@"content":dic[@"name"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"是否线上盖章",@"content":dic[@"isOnlineStr"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"用章单位",@"content":dic[@"isQp"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"是否过产业园",@"content":dic[@"isIndustrialParkStr"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"所属业务",@"content":dic[@"beloneBusiness"] ,@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"采购范围",@"content":dic[@"purchaseScope"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"合同类型",@"content":dic[@"contractType"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"主从合同",@"content":dic[@"psContract"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"采购材料",@"content":dic[@"purchaseMaterial"],@"special":@"0"}];
    if ([dic[@"isOnlineStr"] isEqualToString:@"否"]) {
         [self.basicInformationArr addObject:@{@"title":@"盖章份数",@"content":[dic[@"contractCount"] stringValue],@"special":@"0"}];
    }
    [self.basicInformationArr addObject:@{@"title":@"申请事由",@"content":dic[@"applyReason"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"备注",@"content":dic[@"remark"],@"special":@"0"}];
    
    [self.basicInformationArr addObject:@{@"title":@"项目信息",@"content":@" ",@"special":@"1"}];
    [self.basicInformationArr addObject:@{@"title":@"项目名称",@"content":dic[@"proName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"项目编码",@"content":dic[@"proCode"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"项目线条",@"content":dic[@"proLine"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"工程地址",@"content":dic[@"proAddress"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"所属部门",@"content":dic[@"proDeptName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"项目经理",@"content":dic[@"proManagerName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"项目性质",@"content":@" ",@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"是否重点项目",@"content":dic[@"isKeyProjectStr"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"工程管理模式",@"content":dic[@"proManageMode"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"供应商信息",@"content":@" ",@"special":@"1"}];
    [self.basicInformationArr addObject:@{@"title":@"供应商名称",@"content":dic[@"franName"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"主要联系人",@"content":dic[@"franContacts"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"联系方式",@"content":dic[@"franMobile"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"合同信息",@"content":@" ",@"special":@"1"}];
    [self.basicInformationArr addObject:@{@"title":@"合同总价/暂定(元)",@"content":[dic[@"contractPrice"] stringValue],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"是否为公司版本",@"content":dic[@"isCompanyVersionStr"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"是否为年度协议合同",@"content":dic[@"isYearContractStr"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"签订日期",@"content":dic[@"signDate"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"安装方式",@"content":dic[@"installMethod"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"正文页次",@"content":dic[@"textPage"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"附件页次",@"content":dic[@"attachmentPage"],@"special":@"0"}];
    if ([dic[@"isQp"] isEqualToString:@"全品"] ||[dic[@"isQp"] isEqualToString:@"蘑菇加"]) {
        [self.basicInformationArr addObject:@{@"title":@"全品/蘑菇加合同总价",@"content":dic[@"qpContractPrice"],@"special":@"0"}];
    }
    [self.basicInformationArr addObject:@{@"title":@"合同主要付款条件",@"content":dic[@"contractCondition"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"合同条款说明",@"content":dic[@"contractExplain"],@"special":@"0"}];
    [self.basicInformationArr addObject:@{@"title":@"附件",@"content":@" ",@"special":@"2"}];
    self.dataSourceArray = self.basicInformationArr;
   
}
//在数据加载完后，从新设置定位位置
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self markSectionHeaderLocation];
}
# pragma mark -- tableView deletage
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0){
        NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", (long)indexPath.section,(long)indexPath.row];//以indexPath来唯一确定cell
        YSFlowFormListCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
        
        if (cell == nil) {
            cell = [[YSFlowFormListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        [cell setNewFlowDetailWithDictionary:self.dataSourceArray[indexPath.row]];
        return cell;
    }else if (indexPath.section == 1) {
        YSFlowRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellPostscriptList]; //出列可重用的cell
        if (cell == nil) {
            cell = [[YSFlowRecordListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellPostscriptList];
        }
        if (self.postscriptArray.count > 0) {
            YSFlowRecordListModel *cellModel = self.postscriptArray[indexPath.row];
            [cell setRecordListCellModel:cellModel andIndexPath:indexPath];
            [self callPhone:cellModel.creator andTableViewCell:cell];
        }
        
        return cell;
    }else if(indexPath.section == 2){
        YSFlowRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellExaminationList]; //出列可重用的cell
        if (cell == nil) {
            cell = [[YSFlowRecordListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellExaminationList];
        }
        if (self.recordArray.count > 0) {
            YSFlowRecordListModel *cellModel = self.recordArray[indexPath.row];
            [cell setRecordListCellModel:cellModel andIndexPath:indexPath];
            [self callPhone:cellModel.userId andTableViewCell:cell];
        }
        return cell;
    }else{
        YSFlowRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifierFlow]; //出列可重用的cell
        if (cell == nil) {
            cell = [[YSFlowRecordListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierFlow];
        }
        if (self.turnArray.count > 0) {
            YSFlowRecordListModel *cellModel = self.turnArray[indexPath.row];
            [cell setRecordListCellModel:cellModel andIndexPath:indexPath];
            [self callPhone:cellModel.userId andTableViewCell:cell];
        }
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
    if (indexPath.section == 0) {
        NSDictionary *dic = self.dataSourceArray[indexPath.row];
        if ([dic[@"special"] isEqualToString:@"2"]) {
            YSAttachmentClassificationVC *attachmentClassificationVC = [[YSAttachmentClassificationVC alloc]init];
            attachmentClassificationVC.dataSourceArray = self.attachmentArray;
            [self.navigationController pushViewController:attachmentClassificationVC animated:YES];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
